package com.example.newheze;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.example.newheze.FieldMessage.AnalogData;
import com.example.newheze.FieldMessage.CurrentData;
import com.example.newheze.Monitor.MonitorService;
import com.example.newheze.Util.HttpUtils;
import com.example.newheze.vo.Info;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends MyActivity implements View.OnClickListener {
    EditText admin;
    EditText password;
    TextView textView;
    Button button;
    Intent intent;
    CurrentData currentData;
    CheckBox checkBox;
    Info data;
    private String[] date_soil_humi1= new String[7];
    private float[] info_soil_humi1=new float[7];
    private String[] date_soil_temp1= new String[7];
    private float[] info_soil_temp1=new float[7];
    private String[] date_soil_elect1= new String[7];
    private float[] info_soil_elect1=new float[7];
    private String[] date_soil_humi2= new String[7];
    private float[] info_soil_humi2=new float[7];
    private String[] date_soil_temp2= new String[7];
    private float[] info_soil_temp2=new float[7];
    private String[] date_soil_elect2= new String[7];
    private float[] info_soil_elect2=new float[7];
    private String[] date_sunlight= new String[7];
    private float[] info_sunlight=new float[7];
    private String[] date_co2= new String[7];
    private float[] info_co2=new float[7];
    private String[] date_air_humi= new String[7];
    private float[] info_air_humi=new float[7];
    private String[] date_air_temp= new String[7];
    private float[] info_air_temp=new float[7];
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void setRootView() {
        super.setRootView();
        checkLog();
        setContentView(R.layout.activity_welcome);
        Window window = this.getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(getResources().getColor(R.color.color_green700));
        Intent intent=new Intent(this, MonitorService.class);
        //startService(intent);
        new MyAsyncTask().execute("http://202.194.131.174:8080/wudihoutai/Wenshi_wudi_1_servlet?DTU=001&num=7");

        admin=findViewById(R.id.admin);
        textView=findViewById(R.id.errordata);
        checkBox=findViewById(R.id.box1);
        password=findViewById(R.id.password);
        button=findViewById(R.id.email_sign_in_button);
        button.setOnClickListener(this);

    }
    protected void initWidght() {
        super.initWidght();
        setTitleName("登录");
//                  setTitleBack(false,R.mipmap.ic_search);//有图标，但不是返回
        //setTitleBack(true,0);//返回
//                  setTitleRightText("保存");//右侧文字

        //goneTitleRes(R.id.title_add);隐藏图标，一般用不到
    }
    public void onClick(View v) {
        if (v.getId() == R.id.email_sign_in_button){
            // workFragment.titleSearch();
            Log.d("login1", "onClick: "+admin.getText().toString()+" "+password.getText().toString());

            if(admin.getText().toString().equals("")&&password.getText().toString().equals("")){
                    textView.setVisibility(View.INVISIBLE);
                    if (checkBox.isChecked())
                    {
                        SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
                        editor.putString("log_flag","wudi");
                        editor.apply();
                    }
                    startActivity(intent);
                    this.finish();
            }
            else {
                password.setText("");
                textView.setVisibility(View.VISIBLE);
            }

        }
    }

    private void checkLog()
    {
        SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
        Log.i("showLog_tag", "checkLog: "+sharedPreferences.getString("log_flag","nothing"));
        if (sharedPreferences.getString("log_flag","nothing")!="nothing")
        {
            intent=new Intent(this,MainActivity.class);
            startActivity(intent);
        }
    }

    public class MyAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(s);
                for(int i=0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    data = new Info(jsonObject.getString("datetime"), jsonObject.getString("soil_humi1"), jsonObject.getString("soil_temp1"), jsonObject.getString("soil_elect1"), jsonObject.getString("soil_humi2"), jsonObject.getString("soil_temp2"), jsonObject.getString("soil_elect2"), jsonObject.getString("sunlight"), jsonObject.getString("co2"), jsonObject.getString("air_humi"), jsonObject.getString("air_temp"), jsonObject.getString("DTU"));
                    date_soil_humi1[i]=data.getDatetime();
                    info_soil_humi1[i]= Float.parseFloat(data.getSoil_humi1());
                    date_soil_temp1[i]=data.getDatetime();
                    info_soil_temp1[i]= Float.parseFloat(data.getSoil_temp1());
                    date_soil_elect1[i]=data.getDatetime();
                    info_soil_elect1[i]= Float.parseFloat(data.getSoil_elect1());
                    date_soil_humi2[i]=data.getDatetime();
                    info_soil_humi2[i]= Float.parseFloat(data.getSoil_humi2());
                    date_soil_temp2[i]=data.getDatetime();
                    info_soil_temp2[i]= Float.parseFloat(data.getSoil_temp2());
                    date_soil_elect2[i]=data.getDatetime();
                    info_soil_elect2[i]= Float.parseFloat(data.getSoil_elect2());
                    date_sunlight[i]=data.getDatetime();
                    info_sunlight[i]= Float.parseFloat(data.getSunlight());
                    date_co2[i]=data.getDatetime();
                    info_co2[i]= Float.parseFloat(data.getCo2());
                    date_air_humi[i]=data.getDatetime();
                    info_air_humi[i]= Float.parseFloat(data.getAirhumi());
                    date_air_temp[i]=data.getDatetime();
                    info_air_temp[i]= Float.parseFloat(data.getAirtemp());
                }
                AnalogData analogData=new AnalogData();

                analogData.setDate_soil_humi1(date_soil_humi1);
                analogData.setInfo_soil_humi1(info_soil_humi1);
                analogData.setDate_soil_temp1(date_soil_temp1);
                analogData.setInfo_soil_temp1(info_soil_temp1);
                analogData.setDate_soil_elect1(date_soil_elect1);
                analogData.setInfo_soil_elect1(info_soil_elect1);
                analogData.setDate_soil_humi2(date_soil_humi2);
                analogData.setInfo_soil_humi2(info_soil_humi2);
                analogData.setDate_soil_temp2(date_soil_temp2);
                analogData.setInfo_soil_temp2(info_soil_temp2);
                analogData.setDate_soil_elect2(date_soil_elect2);
                analogData.setInfo_soil_elect2(info_soil_elect2);
                analogData.setDate_sunlight(date_sunlight);
                analogData.setInfo_sunlight(info_sunlight);
                analogData.setDate_co2(date_co2);
                analogData.setInfo_co2(info_co2);
                analogData.setDate_air_humi(date_air_humi);
                analogData.setInfo_air_humi(info_air_humi);
                analogData.setDate_air_temp(date_air_temp);
                analogData.setInfo_air_temp(info_air_temp);

                Bundle mBundle = new Bundle();
                mBundle.putParcelable("Data", analogData);//压入数据
                intent = new Intent();
                intent.setClass(LoginActivity.this, MainActivity.class);
                intent.putExtras(mBundle);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.sendPostMethod(strings[0],"utf-8");
        }
    }
}
